package com.example.utility;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.example.pojo.Product;
import com.example.service.product.ProductService;
import com.example.service.product.impl.ProductServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 工具类
 * @create: 2022-12-09 10:56
 **/

public class ProListener implements ReadListener<Product> {
    private static final int BATCH_COUNT = 100;
    private List<Product> cachedDataList = new ArrayList<>(BATCH_COUNT);
    private ProductService service;


    public ProListener(){
        service = new ProductServiceImpl();
    }
    public ProListener(ProductService service){
        this.service = service;
    }
    @Override
    public void invoke(Product product, AnalysisContext context) {
        System.out.println(product);
        cachedDataList.add(product);
        System.out.println(cachedDataList.size());
        if (cachedDataList.size()>=BATCH_COUNT){
            productData();
            cachedDataList =new ArrayList<>();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        productData();
    }

    private void productData() {
        service.addProductExcel(cachedDataList);
    }
}
    